import java.net.DatagramSocket;
import java.net.SocketException;


public class UDPConnection {
	private DatagramSocket serverSocket;
	private UDPReceiver udpReceiver;
	private GameDataSender gameDataSender;


	public UDPConnection(){
		try {
			serverSocket = new DatagramSocket(Settings.port);
			udpReceiver = new UDPReceiver(serverSocket, this);
			gameDataSender = new GameDataSender(serverSocket, this);
			udpReceiver.start();
			gameDataSender.start();
		} catch (SocketException e) {System.out.println("Could not create datagram socket. Exception: " + e);}
	}
	
	public void stopUDPConnection(){
		try{
			if(udpReceiver != null)
			{
				udpReceiver.stopReceiving();
				udpReceiver = null;
			}
			gameDataSender = null;
			if(serverSocket != null)
				serverSocket.close();
			serverSocket = null;
		}catch(Exception e){System.out.println("Could not close UDP connection: " + e);}
	}
	
	

}
